<template>
    <el-card class="box-card">
        <!-- 
            model 加载表单内部的输入元素操作数据变量，用于后续的数据校验
            rules 定义对应的数据变量的校验要求
         -->
        <el-form :model="user" :rules="userRules" ref="loginForm">
            <!-- prop 以字符串的方式描述 model 中定义的属性名，控制错误操作的提示位置 -->
            <el-form-item prop="username">
                <el-input prefix-icon="User" v-model="user.username" />
            </el-form-item>
            <el-form-item prop="password">
                <el-input prefix-icon="Lock" type="password" show-password v-model="user.password" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="doLogin()">登录</el-button>
                <!-- <el-button type="primary" @click="checkPuzzle()">登录</el-button> -->
            </el-form-item>
        </el-form>

    </el-card>
</template>

<script>
import { ElMessage } from 'element-plus'
import { doUserLogin } from "@/api/userApi.js";
import { userRules } from "@/rules/userRules.js";

export default {
    data() {
        return {
            // name: "",
            // pwd: ""
            user:{
                username:"",
                password:""
            },
            userRules,
            // userRules:{
            //     username:[
            //         { required:true,message:"用户名不能为空", trigger: 'blur' },
            //         { min:3,message:"用户名长度不能小于3",trigger: 'blur' }
            //     ],
            //     password:[
            //         { required:true,message:"密码不能为空",trigger: 'blur' },
            //         { min:3,message:"密码长度不能小于3",trigger: 'blur' }
            //     ]
            // },
            // isShow:false
        }
    },
    methods: {
        // vue3-puzzle-vcode
        // async checkPuzzle(){
        //     await this.$refs.loginForm.validate()
        //     this.isShow = true;
        // },
        async doLogin() {
            await this.$refs.loginForm.validate()

            // await doUserLogin({
            //     username:this.name,
            //     password:this.pwd
            // })
            let result = await doUserLogin(this.user);
            if(result===null) return;
            console.log(result);

            this.$store.commit("setToken",result.token)
            this.$store.commit("setUser",result.user)

            ElMessage.success("登录成功")
            this.$router.push({
                path:"/home"
            })
        }
    },
}
</script>

<style lang="scss" scoped>
.box-card {
    width: 600px;
}
</style>